package com.kedi.welfarecenter.controller.operationcenter;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kedi.welfarecenter.enums.ResultEnum;
import com.kedi.welfarecenter.exception.CommonException;
import com.kedi.welfarecenter.service.operationcenter.CommunityPenSecurityService;
import com.kedi.welfarecenter.utils.ResultVoUtil;
import com.kedi.welfarecenter.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Description:    社区养老保障
* @Author:         ytw
* @CreateDate:     2019/7/23 16:02
* @UpdateRemark:   修改内容
* @Version:        1.0
*/
@RestController
public class CommunityPenSecurityController {
    private static Logger logger = LoggerFactory.getLogger(CommunityPenSecurityController.class);
    @Autowired
    private CommunityPenSecurityService communityPenSecurityService;

    /**
    * 方法实现说明  查找所有社区养老保障列表
    * @author      ytw
    *@param
    * @return
    * @exception
    * @date        2019/7/26 9:34
    */
    @GetMapping("operation/communityPenSecurityList")
    public Map<String, Object> selectcps(@RequestParam(value="page",defaultValue = "1")int page,
                                               @RequestParam(value="size",defaultValue = "20")int size){
        PageHelper.startPage(page,size);
        List<Map<String, Object>> list = communityPenSecurityService.selectcps();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        HashMap<String, Object> data = new HashMap<>();
        data.put("total_size",pageInfo.getTotal());
        data.put("total_page",pageInfo.getPages());
        data.put("current_page",page);
        data.put("data",pageInfo.getList());
        if(list.isEmpty()){
            logger.info("查找所有社区养老保障列表为空");
            throw new CommonException(ResultEnum.LIST_NOT_EXISTED);
        }else{
            return data;
        }

    }

    /**
    * 方法实现说明  搜索框  根据机构名查找
    * @author      ytw
    *@param         welfaretitle
    * @return
    * @exception
    * @date        2019/7/26 9:37
    */
    @GetMapping("operation/communityPenSecurityListBy")
    public Map<String, Object> selectcpsByName(@RequestParam("welfaretitle") String welfaretitle,@RequestParam(value="page",defaultValue = "1")int page,
                                                     @RequestParam(value="size",defaultValue = "20")int size){
        PageHelper.startPage(page,size);
        List<Map<String, Object>> list = communityPenSecurityService.selectcpsByName(welfaretitle);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        HashMap<String, Object> data = new HashMap<>();
        data.put("total_size",pageInfo.getTotal());
        data.put("total_page",pageInfo.getPages());
        data.put("current_page",page);
        data.put("data",pageInfo.getList());
        if(list.isEmpty()){
            logger.info("查找所有社区养老保障列表为空");
            throw new CommonException(ResultEnum.LIST_NOT_EXISTED);
        }else{
            return data;
        }
    }

    /**
    * 方法实现说明  根据社区养老主键id 查找详情
    * @author      ytw
    *@param         welfareId
    * @return
    * @exception
    * @date        2019/7/26 9:40
    */
    @GetMapping("operation/communityPenSecurityListDetails")
    public Map<String, Object> selectcpsDetails(@RequestParam("welfareId") Integer welfareId,@RequestParam(value="page",defaultValue = "1")int page,
                                                      @RequestParam(value="size",defaultValue = "20")int size){
        PageHelper.startPage(page,size);
        List<Map<String, Object>> list = communityPenSecurityService.selectcpsDetails(welfareId);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        HashMap<String, Object> data = new HashMap<>();
        data.put("total_size",pageInfo.getTotal());
        data.put("total_page",pageInfo.getPages());
        data.put("current_page",page);
        data.put("data",pageInfo.getList());
        if(list.isEmpty()){
            logger.info("查找所有社区养老保障列表为空");
            throw new CommonException(ResultEnum.LIST_NOT_EXISTED);
        }else{
            return data;
        }
    }

    /**
    * 方法实现说明  根据领取人员表主键 返回领取人电话
    * @author      ytw
    *@param         welfarereceiverid
    * @return
    * @exception
    * @date        2019/7/26 9:41
    */
    @GetMapping("operation/communityPenSecurityPhone")
    public Map<String, Object> selectcpsPhone(@RequestParam("welfarereceiverid") Integer welfarereceiverid){
        Map<String, Object> map = communityPenSecurityService.selectcpsPhone(welfarereceiverid);
        if(map.isEmpty()){
            logger.info("查找所有社区养老保障列表为空");
            throw new CommonException(ResultEnum.LIST_NOT_EXISTED);
        }else{
            return map;
        }
    }

    /**
    * 方法实现说明   新增社区养老保障事项
    * @author      ytw
    *@param         welfaretitle,lowage,heighage,name,price,givetime
    * @return
    * @exception
    * @date        2019/7/26 9:48
    */
    @PostMapping("operation/communityPenSecurityCreatecps")
    public ResultVo createcps(@RequestParam(value = "welfaretitle") String welfaretitle, @RequestParam("lowage") Integer lowage,
                              @RequestParam("heighage") Integer heighage, @RequestParam("name") String name,
                              @RequestParam(value = "price",required = false,defaultValue ="") Integer price, @RequestParam("givetime") String givetime){
        Integer createcps = communityPenSecurityService.createcps(welfaretitle, lowage, heighage, name, price, givetime);
        if(createcps==0){
            logger.error("新增社区养老保障事项失败");
            throw new CommonException(ResultEnum.SAVE_DATA_ERROR);
        }
        return  ResultVoUtil.success(createcps);
    }

    /**
    * 方法实现说明  根据福利表主键修改
    * @author      ytw
    *@param        welfaretitle,lowage,heighage,name(现金，或者什么物品),price(现金多少钱，或者物品值多少钱),givetime,welfareid
    * @return
    * @exception
    * @date        2019/7/26 9:59
    */
    @PostMapping("operation/communityPenSecurityUpdatecps")
    public ResultVo updatecps(@RequestParam(value = "welfaretitle") String welfaretitle,@RequestParam("lowage") Integer lowage,
                             @RequestParam("heighage") Integer heighage,@RequestParam("name") String name,
                             @RequestParam(value = "price",required = false,defaultValue ="") Integer price,@RequestParam(value = "givetime",required = false) String givetime,
                             @RequestParam("welfareid") Integer welfareid){
        Integer updatecps = communityPenSecurityService.updatecps(welfaretitle, lowage, heighage, name, price, givetime, welfareid);
        if(updatecps==0){
            logger.info("修改社区养老保障事项失败");
            throw new CommonException(ResultEnum.SAVE_DATA_ERROR);
        }
            return  ResultVoUtil.success(updatecps);
    }


}
